//
//  SettingViewController.m
//  soccer
//
//  Created by 上官千城 on 15/9/2.
//  Copyright (c) 2015年 上官千城. All rights reserved.
//

#import "OSettingViewController.h"

@interface OSettingViewController ()

@end

@implementation OSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"我的";
    //创建个人设置数据库

    self.userInfo = [OUserInfo getInfo];
    
    //创建tableView
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-110) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];
    
    //背景图片
    UIImageView *backImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT/2  )];
    
    backImage.userInteractionEnabled = YES;
    backImage.image = [UIImage imageNamed:@"mmaaa"];
    backImage.contentMode = UIViewContentModeScaleToFill;
    backImage.autoresizesSubviews = YES;

//    backImage.image = [self getPartOfImage:[UIImage imageNamed:@"mmaaa"] rect:CGRectMake(0, 20, backImage.frame.size.width, backImage.frame.size.height)];
    [self.view addSubview:backImage];
    [self.view addSubview:self.tableView];
    //创建头文件
    [self creatHeaderView];
    //二维码
    UIButton *rightButton = [UIButton  buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"erweima"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(settingQR) forControlEvents:UIControlEventTouchUpInside];
    rightButton.frame = CGRectMake(0, 0, 20, 20);
    UIBarButtonItem *rigth = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rigth;
    
    //取消多余换航线
    [self.tableView setTableFooterView:[[UIView alloc] initWithFrame:CGRectZero]];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(150, 300, 200, 200);
    button.backgroundColor = [UIColor redColor];
    [button setTitle:@"夜间" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
//    [self.view addSubview:button];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshInfo) name:@"refreshInfo" object:nil];
    
    
}


-(void)refreshInfo
{
    self.userName.text = self.userInfo.nickName;
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:self.userInfo.icon]];
    NSLog(@"newNickName == %@",self.userInfo.nickName);
    [self.tableView reloadData];
}


//创建头文件
-(void)creatHeaderView
{
        //头部View
//    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 250)];
    UIView *aView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, 250*( [UIScreen mainScreen].bounds.size.height / 667))];
    aView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = aView;
    //头像View
    
    
    
    UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 180*(HEIGHT / 667), WIDTH, 70 *HEIGHT/667)];
    headView.backgroundColor = [UIColor grayColor];
    [aView addSubview:headView];

    //个人头像
  
    self.userImage = [[UIImageView alloc]initWithFrame:CGRectMake(30* Proportion, aView.frame.size.height - headView.frame.size.height *5/3, 100*Proportion, 100*Proportion)];
    //    userImage.backgroundColor = [UIColor cyanColor];
    self.userImage.layer.cornerRadius = self.userImage.frame.size.width/2;
    self.userImage.layer.masksToBounds = YES;
    self.userImage.userInteractionEnabled = YES;
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:self.userInfo.icon]];
    [aView addSubview:self.userImage];
    
    //头像轻拍手势
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapHeadAction)];
    [self.userImage addGestureRecognizer:tap];
    //个人名称
    self.userName = [[UILabel alloc]initWithFrame:CGRectMake(self.userImage.frame.origin.x + self.userImage.frame.size.width + 20, - 30, 200* Proportion, 20)];
//        userName.backgroundColor = [UIColor brownColor];
    self.userName.text = self.userInfo.nickName;
    self.userName.font = [UIFont boldSystemFontOfSize:18* Proportion];
    self.userName.textColor = [UIColor whiteColor];
    [headView addSubview:self.userName];
    //性别
    UIImageView *sexImage = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH - (40* Proportion), self.userName.frame.origin.y/2, 30* Proportion, 30* Proportion)];
    //    sexImage.backgroundColor = [UIColor blueColor];
    sexImage.image = [UIImage imageNamed:@"注册详情页(切图)_18@2x"];
    [headView addSubview:sexImage];
    //发布消息
    UILabel *push = [[UILabel alloc]initWithFrame:CGRectMake(self.userName.frame.origin.x-10 , self.userImage.frame.size.height/2 - 20, 90 * Proportion, 20)];
    push.text = @"我发布的消息";
    push.userInteractionEnabled = YES;
    push.font = [UIFont systemFontOfSize:14* Proportion];
    push.textAlignment = NSTextAlignmentCenter;
    //    push.backgroundColor = [UIColor redColor];
    [headView addSubview:push];
    //发布数量
    UILabel *pushNum = [[UILabel alloc]initWithFrame:CGRectMake(push.frame.origin.x, push.frame.origin.y - 20, push.frame.size.width, push.frame.size.height)];
    //    pushNum.backgroundColor = [UIColor yellowColor];
    pushNum.font = [UIFont boldSystemFontOfSize:18* Proportion];
    pushNum.text = @"12";
    pushNum.userInteractionEnabled = YES;
    pushNum.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:pushNum];
    //line
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(push.frame.origin.x+push.frame.size.width + 15, pushNum.frame.origin.y+5, 1, headView.frame.size.height-20)];
    line.backgroundColor = [UIColor lightGrayColor];
    [headView addSubview:line];
    //参与评论
    UILabel *commnet = [[UILabel alloc]initWithFrame:CGRectMake(push.frame.origin.x+ push.frame.size.width + 30, push.frame.origin.y , push.frame.size.width, push.frame.size.height)];
    commnet.text = @"我的评论";
    commnet.textAlignment = NSTextAlignmentCenter;
    commnet.userInteractionEnabled = YES;
    commnet.font = [UIFont systemFontOfSize:14* Proportion];
    //    commnet.backgroundColor = [UIColor redColor];
    [headView addSubview:commnet];
    //评论数量
    UILabel *commnetNum = [[UILabel alloc]initWithFrame:CGRectMake(commnet.frame.origin.x, commnet.frame.origin.y - 20, commnet.frame.size.width, commnet.frame.size.height)];
    //    commnetNum.backgroundColor = [UIColor yellowColor];
    commnetNum.font = [UIFont boldSystemFontOfSize:18* Proportion];
    commnetNum.userInteractionEnabled = YES;
    commnetNum.text = @"123";
    commnetNum.textAlignment = NSTextAlignmentCenter;
    [headView addSubview:commnetNum];
    //我的评论触碰手势
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap2Action)];
    [commnetNum addGestureRecognizer:tap2];
    [commnet addGestureRecognizer:tap2];
    //我的发布消息手势
    UITapGestureRecognizer *tap1 = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tap1Action)];
    [push addGestureRecognizer:tap1];
    [pushNum addGestureRecognizer:tap1];
    
}

//我的评论触碰方法
-(void)tap2Action
{
    NSLog(@"我的评论");
    OMainCommentController *view = [[OMainCommentController alloc]init];
    [self.navigationController pushViewController:view animated:YES];
    
}
//生成二维码
-(void)settingQR
{
    NSLog(@"生成二维码")
    OMainDetailViewController *view = [[OMainDetailViewController alloc]init];
    view.type = 3;
    [self.navigationController pushViewController:view animated:YES];
    
    
    
}


//我发布的消息手势方法
-(void)tap1Action
{
    NSLog(@"我发布的消息");
}

//tableview协议方法
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //关注的球队和球员
        cell.textLabel.text = @"关注的球队和球员";
        cell.textLabel.font = [UIFont systemFontOfSize:15* Proportion];
        return cell;
    }
    
    if (indexPath.section == 1 && indexPath.row == 0) {
        static NSString *cellInfo = @"myTeamCell";
        OSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellInfo];
        if (cell == nil) {
            cell = [[OSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellInfo];
            
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        static NSString *cellinft = @"myPlayerCell";
        OSettingCell *cell = [tableView dequeueReusableCellWithIdentifier:cellinft];
        if (cell ==nil) {
            cell = [[OSettingCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellinft];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }

    if (indexPath.section == 2 && indexPath.row == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        //cell右侧 按钮
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"我的2_12@2x"]];
        [cell addSubview:image];

        return cell;
    }
    if (indexPath.section == 2 && indexPath.row == 1) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        //cell右侧 按钮
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"我的2_12-02@2x"]];
        [cell addSubview:image];
        return cell;
    }
    if (indexPath.section == 3 && indexPath.row == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc]init];
        //cell右侧 按钮
        UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"我的2_12-03@2x"]];
        [cell addSubview:image];
        return cell;
    }
        if (indexPath.section == 3 && indexPath.row == 1) {
            UITableViewCell *cell = [[UITableViewCell alloc]init];
            //cell右侧 按钮
            UIImageView *image = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"我的2_12-04@2x"]];
            [cell addSubview:image];
            return cell;
        }
    return nil;
}
//设置行数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    
    return 2;

}
//设置区数
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
//点击触发方法
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 1 && indexPath.row == 0) {
        NSLog(@"球队");
        OChooseTeamController *view = [[OChooseTeamController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
    }
    if (indexPath.section == 1 && indexPath.row == 1) {
        NSLog(@"球星");
        OChoosePlayerController *view = [[OChoosePlayerController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
    }
   
    if (indexPath.section == 2 && indexPath.row == 0) {
        NSLog(@"我的账号");
        OAccountController *view = [[OAccountController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
    }
    if (indexPath.section == 2 && indexPath.row == 1) {
        NSLog(@"偏好");
        OPreferenceController *view = [[OPreferenceController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
    }
    if (indexPath.section == 3 && indexPath.row == 0) {
        NSLog(@"关于");
        OAboutViewController *view = [[OAboutViewController alloc]init];
        [self.navigationController pushViewController:view animated:YES];
    }
    if (indexPath.section == 3 && indexPath.row == 1) {
        NSLog(@"退出?");
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"确定退出?" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
        alert.delegate = self;
        [alert show];
    }
}

//tap头像进行设置
-(void)tapHeadAction
{
    OMainController *view = [[OMainController alloc]init];
    [self.navigationController pushViewController:view animated:YES];
}

//设置行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0 ) {
        return 30* Proportion;
    }
    if (indexPath.section == 1) {
        return 100* Proportion;
    }
    return 40* Proportion;
}

//alert协议方法
-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        NSLog(@"退出");
        OLoginViewController *view = [[OLoginViewController alloc]init];
        NSUserDefaults *userDefatluts = [NSUserDefaults standardUserDefaults];
        NSDictionary *dictionary = [userDefatluts dictionaryRepresentation];
        for(NSString* key in [dictionary allKeys]){
            if ([key isEqualToString:@"firstLaunch"]) {
                NSLog(@"key = %@",key);
                [userDefatluts removeObjectForKey:key];
                //                    [userDefatluts synchronize];
            }
            
        }
        [self presentViewController:view animated:YES completion:^{
            self.view.window.rootViewController = view;
            

        }];
//        [self.navigationController pushViewController:view animated:YES];
    }
    if (buttonIndex == 1) {
        NSLog(@"取消");
    }
}

-(void)buttonAction
{
    NSLog(@"24");
    NSString *str = [[NSUserDefaults standardUserDefaults]objectForKey:@"status"];
    
    if ([str isEqualToString:@"on"]) {
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeToNiget" object:@"off"];
    }
    else
    {
        
        [[NSNotificationCenter defaultCenter]postNotificationName:@"changeToNiget" object:@"on"];
        
    }

    

}

-(void)nightChange {
    [self.myDelegate nightChange];
    NSLog(@"1111");
}

//图片截取区域
- (UIImage *)getPartOfImage:(UIImage *)img rect:(CGRect)partRect
{
    CGImageRef imageRef = img.CGImage;
    CGImageRef imagePartRef = CGImageCreateWithImageInRect(imageRef, partRect);
    UIImage *retImg = [UIImage imageWithCGImage:imagePartRef];
    //    CGImageRelease(imagePartRef);
    return retImg;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
